<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="字典名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入字典名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="字典值" prop="value">
        <el-input
          v-model="queryParams.value"
          placeholder="请输入字典值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="字段名" prop="filedName">
        <el-input
          v-model="queryParams.filedName"
          placeholder="请输入字段名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注信息" prop="remarkInfo">
        <el-input
          v-model="queryParams.remarkInfo"
          placeholder="请输入备注信息"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否可用" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择是否可用"
          clearable
        >
          <el-option label="可用" :value="1"></el-option>
          <el-option label="不可用" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="small"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="small"
          @click="handleAdd"
          v-hasPermi="['dict:dictInfo:add']"
          >新增</el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="dictInfoList"
      row-key="id"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column label="字典名称" align="left" prop="name" />
      <el-table-column label="字典类型" align="center" prop="type">
        <template slot-scope="scope">
          {{ scope.row.type == 1 ? "父级" : "子级" }}
        </template>
      </el-table-column>
      <el-table-column label="字典值" align="center" prop="value" />
      <el-table-column label="字段名" align="center" prop="filedName" />
      <el-table-column label="备注信息" align="center" prop="remarkInfo" />
      <el-table-column label="前端状态" align="center" prop="statusStr">
      </el-table-column>
      <el-table-column label="小程序状态" align="center" prop="aptitudeStatusStr">
      </el-table-column>
      <el-table-column label="任务状态" align="center" prop="taskStatusStr">
      </el-table-column>
      <el-table-column label="排序权重" align="center" prop="sortWeight" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dict:dictInfo:edit']"
            >修改</el-button
          >
          <!-- <el-button
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dict:dictInfo:remove']"
            >删除</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改用户资质字典管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="字典名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入字典名称" />
        </el-form-item>
        <el-form-item label="字典值" prop="value">
          <el-input v-model="form.value" placeholder="请输入字典值" />
        </el-form-item>
        <el-form-item label="字段名" prop="filedName">
          <el-input
            v-model="form.filedName"
            placeholder="字段名"
          />
        </el-form-item>

        <el-form-item label="前端状态" prop="status">
          <el-select
          v-model="form.status"
          placeholder="请选择是否可用"
        >
          <el-option label="可用" value="1"></el-option>
          <el-option label="不可用" value="0"></el-option>
        </el-select>
        <p>是否在H5或者客户端的位置显示</p>
        </el-form-item>

         <el-form-item label="任务状态" prop="taskStatus">
          <el-select
          v-model="form.taskStatus"
          placeholder="请选择是否可用"
        >
          <el-option label="可用" value="1"></el-option>
          <el-option label="不可用" value="0"></el-option>
        </el-select>
        <p>是否在任务设置的位置显示</p>
        </el-form-item>
        <el-form-item label="小程序任务状态" prop="aptitudeStatus">
          <el-select
            v-model="form.aptitudeStatus"
            placeholder="请选择是否可用"
          >
            <el-option label="可用" value="1"></el-option>
            <el-option label="不可用" value="0"></el-option>
          </el-select>
          <p>是否在小程序设置的位置显示</p>
        </el-form-item>

        <el-form-item label="备注信息" prop="remarkInfo">
          <el-input
            v-model="form.remarkInfo"
            placeholder="请输入备注信息"
          />
        </el-form-item>
        <el-form-item label="排序权重" prop="sortWeight">
          <el-input-number :min="0" style="width: 100%;" v-model="form.sortWeight" placeholder="请输入排序权重" />
        </el-form-item>
        <el-form-item label="父级ID" prop="parentId">
          <treeselect
                v-model="form.parentId"
                :options="treeselectList"
                :normalizer="normalizer"
                :show-count="true"
                placeholder="选择父级ID"
              />
          <!-- <el-input v-model="form.parentId" placeholder="请输入父级ID" /> -->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDictInfo,
  getDictInfo,
  delDictInfo,
  addDictInfo,
  updateDictInfo,
} from "@/api/dict/dictInfo";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import IconSelect from "@/components/IconSelect";

export default {
  name: "DictInfo",
  components: { Treeselect, IconSelect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户资质字典管理表格数据
      dictInfoList: [],
      treeselectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        name: null,
        value: null,
        filedName: null,
        remarkInfo: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      };
    },
    /** 查询菜单下拉树结构 */
    getTreeselect() {
      listDictInfo().then(response => {
        this.treeselectList = [];
        const data = { id: 0, name: '顶级', children: [] };
        data.children = this.handleTree(response.rows, "id");
        this.treeselectList = [data];
      });
    },
    /** 查询用户资质字典管理列表 */
    getList() {
      this.loading = true;
      listDictInfo(this.queryParams).then((response) => {
        this.dictInfoList = this.handleTree(response.rows, "id");
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        value: null,
        filedName: null,
        remarkInfo: null,
        status: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.menuId) {
        this.form.parentId = row.menuId;
      } else {
        this.form.parentId = 0;
      }
      this.open = true;
      this.title = "添加用户资质字典管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      this.getTreeselect();
      getDictInfo(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户资质字典管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateDictInfo(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDictInfo(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除用户资质字典管理编号为"' + ids + '"的数据项？')
        .then(function () {
          return delDictInfo(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "dict/dictInfo/export",
        {
          ...this.queryParams,
        },
        `dictInfo_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
